package com.tushu.view.common;

import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.tushu.dao.StudentDao;
import com.tushu.model.Student;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Setting extends JPanel {
	private JTextField jt1;
	private JTextField jt2;
	private JPasswordField jt3;
	private Student student;
	private StudentDao studentDao=new StudentDao();

	/**
	 * Create the panel.
	 */
	public Setting(Student s) {
		this.student=s;
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u8D26\u53F7\uFF1A");
		lblNewLabel.setBounds(122, 65, 58, 15);
		add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u59D3\u540D\uFF1A");
		lblNewLabel_1.setBounds(122, 115, 58, 15);
		add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("\u5BC6\u7801\uFF1A");
		lblNewLabel_2.setBounds(122, 165, 58, 15);
		add(lblNewLabel_2);
		
		jt1 = new JTextField();
		jt1.setBounds(172, 62, 97, 21);
		add(jt1);
		jt1.setColumns(10);
		
		jt2 = new JTextField();
		jt2.setBounds(172, 112, 97, 21);
		add(jt2);
		jt2.setColumns(10);

		jt1.setText(student.getAccount());
		jt2.setText(student.getName());
		
		jt3 = new JPasswordField();
		jt3.setBounds(172, 162, 97, 21);
		add(jt3);
		jt3.setColumns(10);
		
		JButton jb1 = new JButton("\u4FEE\u6539");

		jb1.setBounds(34, 232, 97, 23);
		add(jb1);
		
		JButton jb2 = new JButton("\u4FDD\u5B58");
		jb2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String pswString=String.valueOf(jt3.getPassword());
				if ("".equals(pswString)) {
					JOptionPane.showMessageDialog(null, "请先输入内容！");
				}else {
					student.setPassword(pswString);
					int res=studentDao.updateStudent(student);
					if (res>0) {
						JOptionPane.showMessageDialog(null, "密码修改成功！");
					}else {
						JOptionPane.showMessageDialog(null, "操作失败！");
					}
				}
			}
		});
		jb2.setBounds(172, 232, 97, 23);
		add(jb2);
		
		JButton jb3 = new JButton("\u53D6\u6D88");
		jb3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jb1.setEnabled(true);
				jb2.setEnabled(false);
				jb3.setEnabled(false);
				jt3.setEditable(false);
			}
		});
		jb3.setBounds(305, 232, 97, 23);
		add(jb3);
		
		jb2.setEnabled(false);
		jb3.setEnabled(false);
		jt1.setEditable(false);
		jt2.setEditable(false);
		jt3.setEditable(false);
		jb1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jb1.setEnabled(false);
				jb2.setEnabled(true);
				jb3.setEnabled(true);	
				jt3.setEditable(true);
			}
		});

	}
}
